<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
    >
      <el-form-item label="用戶名称" prop="username">
        <el-input
            v-model="queryParams.username"
            placeholder="请输入用戶名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            round
            size="mini"
            icon="Search"
            @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="RefreshRight" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
        :data="userList"
        v-loading="loading"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="100" align="center"/>
      <el-table-column type="index" label="序号" width="80"/>
      <!-- <el-table-column label="用戶编号" align="center" prop="userId" /> -->
      <el-table-column label="用戶名称" align="center" prop="username"/>
      <!-- <el-table-column label="用户类型" align="center" prop="userType" /> -->
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="角色" align="center" prop="role">
        <template #default="scope">
          {{
            scope.row.role === "admin"
                ? "超级管理员"
                : scope.row.role === "super"
                    ? "超级用户"
                    : "普通用户"
          }}
        </template
        >
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">{{
            scope.row.sex === "0" ? "男" : "女"
          }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @click="handleStatus(scope.row)"
              :plain="true"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
              type="primary"
              icon="Edit"
              circle
              @click="handleUpdate(scope.row)"
          />
          <!-- <el-popconfirm
              title="此操作将永久删除该用户,是否继续?"
              @confirm="delUser(scope.row.id)"
          >
            <template #reference
            >
              <el-button circle type="danger" icon="Delete"
              />
            </template>
          </el-popconfirm> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!--修改用户信息-->
    <el-dialog
        :title="title"
        v-model="open"
        width="600px"
        :append-to-body="true"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用戶名称" prop="username">
          <el-input v-model="form.username" placeholder="请输入用戶名称"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别" prop="sex">
              <el-select
                  v-model="form.sex"
                  placeholder="请选择性别"
                  size="large"
              >
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色信息" style="width: 240px" prop="role">
              <el-select
                  v-model="form.role"
                  placeholder="请选择角色信息"
                  size="large"
                  style="width: 240px"
              >
                <el-option
                    v-for="item in roleNames"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增用户 -->
    <el-dialog v-model="addOpen" width="800px" :append-to-body="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用戶名称" prop="username">
              <el-input
                  v-model="form.username"
                  placeholder="请输入用戶名称"
                  style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input
                  v-model="form.email"
                  placeholder="请输入邮箱"
                  style="width: 240px"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input
                  v-model="form.phone"
                  placeholder="请输入电话"
                  style="width: 200px"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="用户性别" prop="sex">
              <el-select
                  v-model="form.sex"
                  value-key="value"
                  placeholder="请选择性别"
                  size="large"
                  style="width: 240px"
              >
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用戶昵称" prop="nickName">
              <el-input
                  v-model="form.nickName"
                  placeholder="请输入用戶昵称"
                  style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="角色信息" style="width: 240px" prop="role">
          <el-select
              v-model="form.role"
              placeholder="请选择角色信息"
              size="large"
              style="width: 240px"
          >
            <el-option
                v-for="item in roleNames"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="radio" class="ml-4">
            <el-radio :value="1" size="large">正常</el-radio>
            <el-radio :value="2" size="large">停用</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-left: auto">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUser, delUser, getUser, listUser, updateUser} from "@/api/user";
import {dayjs, ElMessageBox} from "element-plus";
export default {
  name: "UserList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息;表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //
      addOpen: false,
      // 状态
      // status: true,
      options: [
        {value: "0", label: "男"},
        {value: "1", label: "女"}
      ],
      roleNames: [
        {
          value: "admin",
          label: "超级管理员"
        },
        {
          value: "user",
          label: "普通角色"
        },
        {
          value: "super",
          label: "超级用户"
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          {required: true, message: "用户名称不能为空", trigger: "blur"}
        ],
        email: [{required: true, message: "邮箱不能为空", trigger: "blur"}],
        phone: [{required: true, message: "电话不能为空", trigger: "blur"}]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatus(row) {
      const data = row;
      console.log("=======================", data);
      updateUser(data).then(response => {
        this.form = response.data;
        // console.log("=======================", this.form.status);
        // if (this.form.status === 1) {
        //   ElMessage({
        //     message: '当前用户已启用',
        //     type: 'success',
        //     plain: true,
        //   })
        // } else {
        //   ElMessage({
        //     message: '当前用户已禁用',
        //     type: 'error',
        //     plain: true,
        //   })
        // }
      });
    },

    /** 查询用户信息;列表 */
    getList() {
      listUser(this.queryParams).then(response => {
        this.userList = response.data;
        console.log("=======================", this.userList);
        this.total = response.total;
        this.loading = false;
      });
    },
    delUser(id) {
      delUser(id).then(response => {
        if (response.success) {
          this.$message({
            message: "修改用户成功!",
            type: "success",
            showClose: true
          });
          this.getList();
        } else {
          this.$message({
            message: "删除用户失败!",
            type: "error",
            showClose: true
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      console.log("=======================", this.addOpen);
      this.addOpen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   username: null,
      //   nickName: null,
      //   email: null,
      //   phone: null,
      //   sex: null,
      //   role: null,
      //   role:null,
      //   password: null,
      // };
      //重置表单
      debugger;
      this.$refs["form"].resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.username=""
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // console.log("=======================点击了", this.open = true)
      this.addOpen = true;
      this.password = "123456";
      this.title = "添加用户信息;";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const userId = row.userId || this.ids;
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              if (response.status === 200) {
                this.$message({
                  message: "修改用户成功!",
                  type: "success",
                  showClose: true
                });
                this.open = false;
                this.getList();
              } else {
                this.$message({
                  message: "修改用户失败!",
                  type: "error",
                  showClose: true
                });
              }
            });
          } else {
            addUser(this.form).then(response => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
          "system/user/export",
          {
            ...this.queryParams
          },
          `user_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>

<style lang="scss" scoped></style>
